package cn.campus.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.campus.dao.ForumReplyMapper;
import cn.campus.dao.UserMapper;
import cn.campus.pojo.Forum_reply;
import cn.campus.pojo.Forum_replyAndUser;
import cn.campus.pojo.User;
import cn.campus.service.ForumReplyService;
@Service
public class ForumReplyServiceImpl implements ForumReplyService {
	@Resource
	private ForumReplyMapper forumReplyMapper;

	@Resource
	private UserMapper userMapper;
	@Override
	public int addForumReply(Forum_reply forum_reply) {
		// TODO Auto-generated method stub
		return forumReplyMapper.insertForumReply(forum_reply);
	}

	@Override
	public int deleteForumReply(String forum_reply_id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Forum_replyAndUser findAllForumReplys(int currentPage, int count) {
		// TODO Auto-generated method stub
		Forum_replyAndUser forum_replyAndUser=new Forum_replyAndUser();
		//查询评论总数
		int total = forumReplyMapper.selectSum();
		forum_replyAndUser.setTotal(total);
		//添加当前页
		forum_replyAndUser.setCurrentPage(currentPage);
		//添加每页的数量
		forum_replyAndUser.setCount(count);
		//查询所有的评论
		List<Forum_reply> forumReplys = forumReplyMapper.selectAllForumReplys(currentPage, count);
		//添加查询到的帖子
		forum_replyAndUser.setForum_replys(forumReplys);
		//根据reply_id，找到评论者
		List<User> users = userMapper.selectAllUsersByReplyID(forum_replyAndUser);
		//根据be_reply_id，找到被评论者
		List<User> usersByBeReplyID = userMapper.selectAllUsersByBeReplyID(forum_replyAndUser);
		users.addAll(usersByBeReplyID);
		//添加user
		forum_replyAndUser.setUsers(users);
		//添加总页数
		forum_replyAndUser.setTotalPage((int)Math.ceil(total/count));

		return forum_replyAndUser;
	}

}
